package com.ibm.commerce.order.facade.server.commands;

import com.ibm.commerce.foundation.common.util.logging.LoggingHelper;
import com.ibm.commerce.order.facade.datatypes.DocumentRoot;
import com.ibm.commerce.order.facade.datatypes.OrderFactory;
import com.ibm.commerce.order.facade.datatypes.ShowOrderDataAreaType;
import com.ibm.commerce.order.facade.datatypes.ShowOrderType;
import com.ibm.commerce.order.objects.OrderAccessBean;
import com.ibm.websphere.command.CommandException;
import java.util.List;
import java.util.logging.Logger;

public class ComposeOrderSummaryCmdImpl extends AbstractComposeOrderCmdImpl
  implements ComposeOrderCmd
{
  public static final String COPYRIGHT = "(c) Copyright International Business Machines Corporation 1996,2008";
  private static final String CLASSNAME = ComposeOrderSummaryCmdImpl.class.getName();

  private static final Logger LOGGER = LoggingHelper.getLogger(ComposeOrderSummaryCmdImpl.class);

  protected ShowOrderType composeOrderSummary(List avOrders)
    throws CommandException
  {
    String METHODNAME = "buildOrderSummary";
    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
      LOGGER.entering(CLASSNAME, "buildOrderSummary", new Object[] { avOrders });
    }

    ShowOrderType showOrder = getOrderFactory().createShowOrderType();
    DocumentRoot docRoot = getOrderFactory().createDocumentRoot();
    docRoot.setShowOrder(showOrder);

    ShowOrderDataAreaType dataArea = getOrderFactory().createShowOrderDataAreaType();
    showOrder.setDataArea(dataArea);

    dataArea.setShow(composeShowType(avOrders));

    boolean abIncludeOrderItems = false;

    for (int i = 0; i < avOrders.size(); i++)
    {
      OrderAccessBean abOrder = (OrderAccessBean)avOrders.get(i);
      dataArea.getOrder()
        .add(composeOrder(abOrder, abIncludeOrderItems));
    }

    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
      LOGGER.exiting(CLASSNAME, "buildOrderSummary", showOrder);
    }
    return showOrder;
  }

  public void execute()
    throws CommandException
  {
    String METHODNAME = "execute()";
    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
      LOGGER.entering(CLASSNAME, "execute()");
    }

    setShowOrder(composeOrderSummary(getOrders()));

    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER))
      LOGGER.exiting(CLASSNAME, "execute()", getShowOrder());
  }
}